const { readFileSync } = require('node:fs')
const { EOL } = require('node:os')
const { join, resolve } = require('node:path')

const HOME = resolve(__dirname)

function main() {
    const content = readFileSync(join(HOME, '..', 'theList.csv'), 'utf-8')
    const lines = content.split(EOL).filter(Boolean)
    const cells = lines.map(line => line.split(','))

    const index = new Map()
    cells.forEach(line => {
        const [ file, decl, flag ] = line
        if (decl) {
            if (!index.has(decl)) {
                index.set(decl, [])
            }
            index.get(decl).push(file)
        }
    })

    console.error("_________ REPEATS _________")
    for (const [decl, files] of index) {
        if (files.length > 1) {
            console.error(decl, files)
        }
    }

    const materialized = []
    const structures = []
    cells.forEach(line => {
        const [ file, decl, flag ] = line
        if (flag) {
            const preparedFlag = flag.trim().toUpperCase()
            const fqName = guessFQName(file, decl)
            if (preparedFlag === 'N') {
                materialized.push([file, decl, fqName])
            }
            if (preparedFlag === 'Y') {
                structures.push([file, decl, fqName])
            }
        }
    })

    console.error("_________ MATERIALIZED _________")
    materialized.forEach(record => {
        console.log(`"${record[2]}",`)
    })

    console.error("_________ STRUCTURES _________")
    structures.forEach(record => {
        console.log(`"${record[2]}",`)
    })
}
main()

///////////////////////////////////////////////////////////////////////////

function guessFQName(file, decl) {
    const trimmed = file.substring(file.indexOf('@') + 1)
    return trimmed.split('/')
        .flatMap(f => f.split('.'))
        .filter(f => f)
        .concat([decl])
        .join('.')

}
